#ifndef VIRTUALFSOBJECT_H
#define VIRTUALFSOBJECT_H


#include <QString>
#include <QMutex>
#include <QVector>
#include <QDebug>
#include <QMap>
#include <QFile>


class VirtualFsObject
{
public:
    VirtualFsObject();
    VirtualFsObject(const QString & virtualWay, const QString & realWay,
                    const QString & name, const QString & realName, bool __isdir);

    ~VirtualFsObject();

    int size();
    VirtualFsObject *at(int i);
    void remove(int i);
    void clear();

    void setVirtualWay(const QString & );
    void setRealWay(const QString & );
    void setName(const QString & );
    void setRealName(const QString & t);
    void setDirType(bool );
    VirtualFsObject *push_back(VirtualFsObject *obj);

    VirtualFsObject * operator [](int i) {
        return at(i);
    }

    inline const QString & virtualWay() const {
        return _virtualWay;
    }

    inline const QString & realWay() const {
        return _realWay;
    }

    inline const QString & name() const {
        return _name;
    }

    inline const QString & realName() const {
        return _realName;
    }

    inline bool isHidden() {
        return _is_hidden;
    }

    inline void setVisible(bool is) {
        _is_hidden = is? false : true;
    }

    inline bool isDir() const {
        return _is_dir;
    }

    QFile *getStreamById(int fd, bool lock = true);
    void bindStream(int fd, QFile *f, bool lock = true);
    void unbindStream(int fd, bool lock = true);
    void clear_io();

    int fileOpen();
    int fileClose(int, bool lock = true);
    QByteArray fileRead(int fd, quint64 sz);
    bool fileIsOpen(int fd);
    qint64 fileSize(int fd);
    qint64 fileTell(int fd);
    bool fileSeek(int fd, qint64 pos);


private:
    QString _virtualWay;
    QString _realWay;
    QString _name;
    QString _realName;
    bool _is_dir;
    bool _is_hidden;
    QVector <VirtualFsObject*> root; // if folder
    QMap <int, QFile *> __io_streams;
    QMutex mutex, io_mtx;
    QByteArray _emprt_arr;
};



#endif // VIRTUALFSOBJECT_H
